package com.uchengwang.ui.cm_ui.cycle.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleBean;
import com.uchengwang.ui.cm_ui.cycle.ui.ApplyCycleActivity;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/5/27.
 */

public class CycleListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<CycleBean> data = new ArrayList<>();
    private boolean isShow;
    public CycleListAdapter(Context context){
        this.context = context;
    }

    public void setData(List<CycleBean> data){
        this.data = data;
        notifyDataSetChanged();
    }

    public List<CycleBean> getData(){
        return data;
    }

    public void setShow(boolean isShow){
        this.isShow = isShow;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_cycle_list, parent, false);
        return new CycleListVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        CycleListVH holder = (CycleListVH) h;
        final CycleBean bean = data.get(position);
        ImageManager.getInstance().loadImage(context, holder.cover, bean.getCycleCover(), ImageManager.ImageType.CIRCLE);
        holder.list_name.setText(bean.getCycleName());
        holder.list_desc.setText(bean.getCycleDesc());

        if (isShow){
            holder.list_sign.setVisibility(View.VISIBLE);
            holder.list_sign.setText("(热圈)");
        }else {
            holder.list_sign.setVisibility(View.GONE);
        }



        if (bean.getUid() == UCommonUtil.getUtil().getId()){
            //自己创建的
            holder.list_enter.setBackgroundResource(R.color.white);
            holder.list_enter.setTextColor(context.getResources().getColor(R.color.comment_color));
            holder.list_enter.setText("已加入");
            holder.list_quanzhu.setVisibility(View.VISIBLE);
        }else {
            holder.list_quanzhu.setVisibility(View.GONE);
            if (bean.getIsjoin() ==null || bean.getIsjoin() ==2){
                holder.list_enter.setBackgroundResource(R.color.white);
                holder.list_enter.setTextColor(context.getResources().getColor(R.color.uni_title_bar));
                holder.list_enter.setText("加入");
            }else {
                switch (bean.getIsjoin()){
                    case 0://0=未审核 1=过 2=不过 3=黑名单
                        holder.list_enter.setBackgroundResource(R.drawable.selector_chat);
                        holder.list_enter.setTextColor(context.getResources().getColor(R.color.register_next_selector));
                        holder.list_enter.setText("等待");
                        break;
                    case 1:
                        holder.list_enter.setBackgroundResource(R.color.white);
                        holder.list_enter.setTextColor(context.getResources().getColor(R.color.comment_color));
                        holder.list_enter.setText("已加入");
                        break;
//                    case 2:
//                        holder.list_enter.setBackgroundResource(R.color.white);
//                        holder.list_enter.setTextColor(context.getResources().getColor(R.color.text_item_color2));
//                        holder.list_enter.setText("被拒绝");
//                        break;
                    case 3:

                        break;
                }
            }
        }


        holder.list_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((bean.getIsjoin() ==null || bean.getIsjoin() ==2 )&& bean.getUid() != UCommonUtil.getUtil().getId()){
                    //点击加入 跳转到加入社圈界面
                    Intent intent = new Intent(context, ApplyCycleActivity.class);
                    intent.putExtra(BaseConfig.PUB_TITLE, bean.getCycleName());
                    intent.putExtra(BaseConfig.ID, bean.getId());
                    context.startActivity(intent);
                }

            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL+"/cycle/"+bean.getId());
//                intent.putExtra(BaseConfig.URL_TITLE, bean.getCycleName());
//                intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                context.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class CycleListVH extends RecyclerView.ViewHolder {
        private ImageView cover, list_quanzhu;
        private TextView list_name, list_desc, list_enter, list_sign;
        private View itemView;

        public CycleListVH(View view) {
            super(view);
            AutoUtils.auto(view);

            itemView = view;

            list_quanzhu = view.findViewById(R.id.list_quanzhu);
            list_desc = view.findViewById(R.id.list_desc);

            cover = view.findViewById(R.id.list_image);
            list_name = view.findViewById(R.id.list_name);
            list_enter = view.findViewById(R.id.list_enter);

            list_sign = view.findViewById(R.id.list_sign);

        }
    }
}
